#include "Palette.h"
#include "SmartArray.h"

#ifndef NULL
#define NULL 0
#endif

#include <iostream>
#include <cmath>
#define cairo_color(a) ((a)/(static_cast<double>(0xFF)))
using std::cout;
using std::endl;

byte Color::round(byte b)
{
	double res = b / 8.0;
	return static_cast<byte>((fmod(b,1.0)>0.5)? ((b>=31)?floor(res):floor(res)+1):floor(res));
}
Color::Color()
{
	Red(0);
	Green(0);
	Blue(0);
}
byte Color::Red() const{ return _RGB.red * 8; }
byte Color::Green() const{  return _RGB.green * 8; }
byte Color::Blue() const{  return _RGB.blue * 8;  }
byte Color::Red(byte r){ return _RGB.red = round(r); }
byte Color::Green(byte g){ return _RGB.green = round(g); }
byte Color::Blue(byte b){ return _RGB.blue = round(b); }
Color& Color::operator = ( unsigned int RGB_ )
{
	Red( (RGB_>>16)&0xFF );
	Green( (RGB_>>8)&0xFF );
	Blue( RGB_&0xFF );
	return *this;
}
Color& Color::operator = (unsigned short v)
{
	Red((v&0x3C00)>>10);
	Green((v&0x03E0)>>5);
	Blue(v&0x001F);
	return *this;
}
Color& Color::operator = ( Color& c)
{
	Red(c.Red());
	Green(c.Green());
	Blue(c.Blue());
	return *this;
}
Color::operator int()
{
	return ((Red()<<16)|Green()<<8|Blue());
}
Color::operator short()
{
	return ((_RGB.red<<10)|(_RGB.green)<<5|(_RGB.blue));
}

Color& Palette::operator [] ( int i) const
{
	return SmartArray<Color>::operator[](i);
}
Palette& Palette::operator = ( Palette& pal )
{
	for( unsigned int i=0; i<16; i++)
	{
		operator[](i) = pal[i];
	}
	return *this;
}
void Palette::ExportToFile(std::ostream& o ) {}
void Palette::LoadFromFile(std::istream& i ) {}
Palette::Palette(const char* _name):SmartArray<Color>(16),name(_name)
{
	for(unsigned int i=0; i<16; i++)
		Append(new Color);
}
const SmartString& Palette::Name() const
{
	return name;
}

PaletteFormat::PaletteFormat(const char* desc)
{
	mDesc.Append(desc);
}
const SmartString& PaletteFormat::Description() const{ return mDesc; }
const SmartArray<SmartString> PaletteFormat::Arguments() const
{
	std::cout << mArguments << std::endl;
	//return mArguments.Explode( SmartString::SplitPoint().AddPoint(';').AddPoint('\n'));
	return SmartArray<SmartString>().Append(new SmartString("string filename"));
}